package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.domain.ShopAuditLog;
import cn.itsource.pethome.org.domain.po.ShopPO;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.mapper.ShopAuditLogMapper;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.util.BaiduAuditUtils;
import cn.itsource.pethome.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-10-09
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    /**
     * 实现商家入驻的业务
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
//        商家入驻的核心业务
        //1.验证参数是否正确(判断是否为null)
        if(shop.getName() == null){
            throw new BusinessException("店铺名称不能为空");//业务异常
        }
        if(shop.getAddress() == null){
            throw new BusinessException("店铺地址不能为空");//业务异常
        }
        //* 店铺的的名字  todo 百度AI智能审核
        if (!BaiduAuditUtils.baiduTextVerify(shop.getName())) {
            throw new BusinessException("您填写的信息不合法，请重新填写");//业务异常
        }
//        * 2.判断当前店铺是否已经被注册
        Long count = shopMapper.countByName(shop.getName());
        if(count > 0){
            throw new BusinessException("该店铺已存在");//业务异常
        }

//        * 3.对参数进行的校验 例如 手机号、用户名、地址等等  你们自己做的
//        * 4.保存管理员
        employeeMapper.insert(shop.getAdmin());
//        * 5.保存商家到shop 设置管理员的外键(id)
        //给店铺设置管理员的id
        shop.setAdminId(shop.getAdmin().getId());//刚才生成的哪个主键id
        shopMapper.insert(shop);

        //给管理员设置店铺的id
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());
    }

    /**
     * 1.点击通过
     *     1.1 记录一个操作日志
     *     1.2 才是修改当前状态
     *     1.3 通过邮箱发送邮件  添加发送激活的超链接
     * @param shopAuditLog
     */
    @Transactional
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //日志记录
        //前端只传入了2个参数  shop_id 和 note
        shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopAuditLog.setAuditId(333L);
        shopAuditLogMapper.insert(shopAuditLog);
        //店铺状态修改
        Shop shop = shopMapper.queryOne(shopAuditLog.getShopId());
        shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopMapper.update(shop);
        //发送邮件
        //查询邮箱
        Employee employee = employeeMapper.queryOne(shop.getAdminId());//当前店铺的管理员信息
        String msg = "<h1>恭喜你审核通过，请求点击链接激活店铺</h1>"+
                "<a src='??????'>点击激活店铺</a>";
        sendEmail(shop.getAdminId(),employee.getEmail(),msg);
    }

    /**
     * 2.点击驳回
     *     2.1 记录一个操作日志
     *     2.2 才是修改当前状态
     *     2.3 通过邮箱发送邮件  直接备注发送给店铺的管理员
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //日志记录
        //前端只传入了2个参数  shop_id 和 note
        shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopAuditLog.setAuditId(333L);
        shopAuditLogMapper.insert(shopAuditLog);
        //店铺状态修改
        Shop shop = shopMapper.queryOne(shopAuditLog.getShopId());
        shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopMapper.update(shop);
        //发送邮件
        //查询邮箱
        Employee employee = employeeMapper.queryOne(shop.getAdminId());//当前店铺的管理员信息
        sendEmail(shop.getAdminId(),employee.getEmail(),shopAuditLog.getNote());
    }

    @Override
    public List<ShopPO> shopRegisterCount() {
        return shopMapper.shopRegisterCount();
    }

    @Autowired
    private JavaMailSender javaMailSender;

    private void sendEmail(Long admin_id,String email,String msg){
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1124322914@qq.com"); helper.setSubject("宠物之家审核结果通知");
            //第二个参数 true 代表是否是一个html内容
            helper.setText(msg,true);
            //添加附件
            //helper.addAttachment("文件名.jpg",new File("C:\\Users\\Administrator\\Desktop\\1130\\学习资料\\80964531_631384930940573_3086659336252948480_o.jpg"));
//        helper.addAttachment("压缩文件", new File("C:\\resources.zip"));
            //收件人
           // String[] s = {"1342027288@qq.com","1124322914@qq.com"};
            helper.setTo(email);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
